import 'package:super_study_english_tool/dao/dao_user_memory_curve.dart';
import 'package:super_study_english_tool/model/model_user_memory_curve.dart';

class ModelUserAlbumMediaRecord {
  int? id;
  int? userId;
  int? albumId;
  int? mediaId;
  int? sentenceId;
  String? sentence;
  int? startTime;
  int? endTime;
  int? status; // -2 忽略  -1 错误 0 需要复习 1 完成
  int? startReviewTime; // 开始复习时间
  int? reviewProgress = 0; // 复习进度
  int? lastReviewTime; // 最后复习时间
  int rightQty = 0;
  int wrongQty = 0;
  int? deferred; // 延期时间
  int? gmtCreate;
  int? gmtModified;
  String? remark;

  // 判断当前是否要复习
  bool needReview() {
    // 忽略的和完成的不需要复习
    if (status != 0) return false;
    // 还没有开始复习
    if (startReviewTime == null) return false;
    // if (lastReviewTime == null) return false;

    // 复习曲线
    List<ModelUserMemoryCurve> curves = DaoUserMemoryCurve.findAll();

    // 计算当前时间与开始复习时间的间隔
    DateTime now = DateTime.now();
    int nowTimeSpan = now.millisecondsSinceEpoch - startReviewTime!;
    // 计算前一次复习时间与开始复习事件的间隔
    int lastTimeSpan = lastReviewTime! - startReviewTime!;

    int curProgress = calcReviewPosition(nowTimeSpan);
    int lastProgress = calcReviewPosition(lastTimeSpan);
    if (curProgress == lastProgress) {
      return false;
    }
    if (curProgress - lastProgress > 1) {
      return true;
    }
    if (curProgress - lastProgress == 1) {
      print("map:" + map.toString());
      if (nowTimeSpan - lastTimeSpan > map[curProgress]! - (map[lastProgress] ?? 0)) {
        return true;
      }
    }

    return false;
  }

  static Map<int, int> map = {};

  static int calcReviewPosition(int timeSpan) {
    // 复习曲线
    if (map.length < 1) {
      List<ModelUserMemoryCurve> curves = DaoUserMemoryCurve.findAll();
      for (ModelUserMemoryCurve c in curves) {
        map[c.reviewProgress!] = c.reviewTime!;
      }
    }

    for (int i = 1; i < 13; i++) {
      if (timeSpan < map[i]!) {
        return i - 1;
      }
    }

    return 13;
  }

  // 计算复习进度
  int calcReviewProgress() {
    int timeSpan = DateTime.now().millisecondsSinceEpoch - this.startReviewTime!;
    return calcReviewPosition(timeSpan);
  }
}
